package com.tacuati.content;

import java.io.RandomAccessFile;

import javax.swing.ImageIcon;

import com.tacuati.Utils;
import com.tacuati.ui.Icons;

public class MobiBook extends Item {
	private String cdetype="";
	private String asin="";
	
	public MobiBook(String path, Format f) {
		super(path);
		String title = "";
		String hash = "";
		String author = "";
		String cdetype = "EBOK";
		String asin = "";
		try {
			RandomAccessFile file = new RandomAccessFile(path, "r");

			byte[] b8 = new byte[8];
			file.seek(60);
			file.read(b8);
			String type = new String(b8, "UTF8");

			byte[] b4 = new byte[4];
			file.seek(78);
			file.read(b4);
			int headerOffset = (int) Utils.byteArrayToInteger(b4);

			file.seek(headerOffset + 20);
			file.read(b4);
			int headerLength = (int) Utils.byteArrayToInteger(b4);

			file.seek(headerOffset + 28);
			file.read(b4);
			int encoding = (int) Utils.byteArrayToInteger(b4);

			String enc = "UTF-8";
			switch (encoding) {
			case 65001:
				enc = "UTF-8";
				break;
			case 1252:
				enc = "CP1252";
			}

			file.seek(headerOffset + 84);
			file.read(b4);
			int titleOffset = (int) Utils.byteArrayToInteger(b4);

			file.seek(headerOffset + 88);
			file.read(b4);
			int titleLength = (int) Utils.byteArrayToInteger(b4);

			byte[] btitle = new byte[titleLength];
			file.seek(headerOffset + titleOffset);
			file.read(btitle);
			title = new String(btitle);

			file.seek(headerOffset + headerLength + 16 + 4);
			file.read(b4);
			int exthLength = (int) Utils.byteArrayToInteger(b4);

			int offset = 12;
			while (offset < exthLength) {
				file.seek(headerOffset + headerLength + 16 + offset);
				file.read(b4);
				int recordType = (int) Utils.byteArrayToInteger(b4);
				offset += 4;
				file.seek(headerOffset + headerLength + 16 + offset);
				file.read(b4);
				int recordLength = (int) Utils.byteArrayToInteger(b4);
				offset += 4;
				byte[] info;

				switch (recordType) {
				case 100:
					file.seek(headerOffset + headerLength + 16 + offset);
					info = new byte[recordLength - 8];
					file.read(info);
					author = new String(info, enc);
					break;
				case 113:
					file.seek(headerOffset + headerLength + 16 + offset);
					info = new byte[recordLength - 8];
					file.read(info);
					asin = new String(info, enc);
					break;
				case 501:
					file.seek(headerOffset + headerLength + 16 + offset);
					info = new byte[recordLength - 8];
					file.read(info);
					cdetype = new String(info, enc);
					break;
				case 503:
					file.seek(headerOffset + headerLength + 16 + offset);
					info = new byte[recordLength - 8];
					file.read(info);
					title = new String(info, enc);
					break;
				default:
					break;
				}
				offset += (recordLength - 8);
			}

			file.close();

			hash = Utils.getHash(path);
			
			this.setHash(hash);
			this.setFormat(f);
			this.setTitle(title);
			this.setAuthor(author);
			this.setPath(path);
			this.setCdetype(cdetype);
			this.setAsin(asin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String toJson() {
		String json = "";
		if (!asin.equals("")){
			json += "\"#" + getHash() + "^"+cdetype+"\"";
		} else {
			json += "\"*" + getHash() + "\"";
		}
		return json;
	}

	@Override
	public Item copy() {
		return new MobiBook(this.getPath(), this.getFormat());
	}

	public String getCdetype() {
		return cdetype;
	}

	public void setCdetype(String cdetype) {
		this.cdetype = cdetype;
	}

	public String getAsin() {
		return asin;
	}

	public void setAsin(String asin) {
		this.asin = asin;
	}
	
	@Override
	public String getHash() {
		if (!asin.equals(""))
			return getAsin();
		else 
			return super.getHash();
	}
	
	@Override
	public ImageIcon getIcon() {
		return Icons.mobiIcon;
	}
	
	@Override
	public String getNodeRenderer() {
		return super.getNodeName() + " - " + getAuthor();
	}
}
